<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    /**
     * Initialize module autoloader
     * 
     * @return Zend_Application_Module_Autoloader
     */
    protected function _initAutoload()
    {
        $autoloader = new Zend_Application_Module_Autoloader(array(
            'namespace' => '',
            'basePath'  => APPLICATION_PATH .'/modules/usermanagement',
            'resourceTypes' => array (
                'form' => array(
                    'path' => 'forms',
                    'namespace' => 'Form',
                ),
                'model' => array(
                    'path' => 'models',
                    'namespace' => 'Model',
                ),
            )
        ));
        return $autoloader;
    }

    /**
     * Set config value to Zend_Registry
     * 
     */
    protected function _initConfig()
    {
        Zend_Registry::set('config', $this->getOptions());
    }
    
    /**
     * Initialize View Options
     * 
     * @return Zend_View
     */
    
    protected function _initView()
    {
        $options = $this->getOptions();
        if (isset($options['resources']['view'])) {
            $view = new Zend_View($options['resources']['view']);
        } else {
            $view = new Zend_View;
        }
        
        if (isset($options['resources']['view']['doctype'])) {
            $view->doctype($options['resources']['view']['doctype']);
        }
        
        if (isset($options['resources']['view']['contentType'])) {
            $view->headMeta()->appendHttpEquiv('Content-Type',
                $options['resources']['view']['contentType']);
        }
        
        if (isset($options['resources']['view']['headTitle'])) {
            $view->headTitle($options['resources']['view']['headTitle']);
        }

        if (isset($options['resources']['view']['setSeparator'])) {
            $view->headTitle()->setSeparator($options['resources']['view']['setSeparator']);
        }
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
            'ViewRenderer'
        );
        $viewRenderer->setView($view);
        $view->addHelperPath('Api/View/Helper/', 'Api_View_Helper');
        return $view;
    }

    /**
     * Start Session, Initialize Seesion path and Session validation
     */
    protected function _initCoreSession()
    {
    	$options = $this->getOptions();
        Zend_Session::setOptions($options['resources']['session']);
        try {
            Zend_Session::start();
        } catch (Zend_Session_Exception $e) {
            Zend_Session::destroy();
            Zend_Session::start();
            Zend_Session::regenerateId();
        }
        Zend_Session::registerValidator(new Zend_Session_Validator_HttpUserAgent());
    }

    /**
     * Initialize Recaptcha
     */
    protected function _initRecaptcha()
    {
        $this->options = $this->getOptions();
        Zend_Registry::set('recaptcha', $this->options['recaptcha']);
    }
 
    protected function _initMail()
    {
        try {
            $config = Zend_Registry::get('config');
            $emailConfig = array(
                'auth' => $config['email']['auth'],
                'username' => $config['email']['username'],
                'password' => $config['email']['password'],
                'ssl' => $config['email']['ssl'],
                'port' => $config['email']['port']
            );
            $server = $config['email']['server'];
            $transport = new Zend_Mail_Transport_Smtp($server, $emailConfig);
            Zend_Mail::setDefaultTransport($transport);
        } catch (Exception $e) {
            throw new exception($e->getMessage());
        }
    }
    
    protected function _initRegisterLogger() 
    {
        try {
            $this->bootstrap('Log');
            if (!$this->hasPluginResource('Log')) {
                throw new Zend_Exception('*** Log is not enabled in config.ini ***'); 
            }
            $logger = $this->getResource('Log');
            if($logger != null) {
                Zend_Registry::set('Log', $logger);
            }  
        } catch (Exception $e) {
            throw new exception($e->getMessage());
        }
    }
    
    protected function _initLogMail()
    {
        try {
            $config = Zend_Registry::get('config');
            if ($this->hasPluginResource('Log')) {
                if ($this->hasPluginResource('Log')) {
                    $resource = $this->getPluginResource('Log');
                }
                
                $mail = new Zend_Mail();
                $mail->setSubject($config['resources']['log']['stream']['mailSubject'])
                        ->setFrom($config['resources']['log']['stream']['mailFrom'],
                                $config['resources']['log']['stream']['mailFromName'])
                        ->addTo($config['resources']['log']['stream']['mailTo']);

                $writer = new Zend_Log_Writer_Mail($mail);
                //$writer->setSubjectPrependText($config['resources']['log']['stream']['mailSubjectPrependText']);

                $log = $resource->getLog();
                $log->addWriter($writer);
            }
        } catch (Exception $e) {
            throw new exception($e->getMessage());
        }
    }

    protected function _initActionHelper()
    {
        // Global Action Helper
        Zend_Controller_Action_HelperBroker::addPath(
            APPLICATION_PATH . '/../library/Api/Controller/Action/Helper',
                'Api_Controller_Action_Helper_');
    }
 
}

